package cn.io0288.blog2.model;

import com.alibaba.fastjson2.JSONObject;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

public class Article {
    public int aid;
    public int uid;
    public int cid;
    public String title;
    public String title2;
    public String img;
    public String img_alt;
    public String content;
    public String date;
    public String lastdate;

    public Article(int aid, int uid, int cid, String title, String title2, String img, String img_alt, String content, String date, String lastdate) {
        this.aid = aid;
        this.uid = uid;
        this.cid = cid;
        this.title = title;
        this.title2 = title2;
        this.img = img;
        this.img_alt = img_alt;
        this.content = content;
        this.date = date;
        this.lastdate = lastdate;
    }

    public Article(ResultSet rs) throws SQLException {
        this.aid = rs.getInt("aid");
        this.uid = rs.getInt("uid");
        this.cid = rs.getInt("cid");
        this.title = rs.getString("title");
        this.title2 = rs.getString("title2");
        this.img = rs.getString("img");
        this.img_alt = rs.getString("img_alt");
        this.content = rs.getString("content");
        this.date = rs.getString("date");
        this.lastdate = rs.getString("lastdate");
    }

    public Article(Map<String, String[]> p) {
        System.out.printf(JSONObject.toJSONString(p));
        this.aid = Integer.parseInt(p.get("aid")[0]);
        this.uid = 1;
        this.cid = Integer.parseInt(p.get("cid")[0]);
        this.title = p.get("title1")[0];
        this.title2 = p.get("title2")[0];
        this.img = p.get("img")[0];
        this.img_alt = p.get("img_alt")[0];
        this.content = p.get("content")[0];
        this.date = "";
        this.lastdate = String.valueOf(System.currentTimeMillis() / 1000);
    }
    public String toString(){
        return ("Article{aid:%d, uid:%d, cid:%d, title:%s, title2:%s, img:%s, img_al:%s, content:%s, data:%s, lastdata:%s}").formatted(
                this.aid,
                this.uid,
                this.cid,
                this.title,
                this.title2,
                this.img,
                this.img_alt,
                this.content,
                this.date,
                this.lastdate
        );
    }
}
